//
//  ViewController.swift
//  CoreImageFun
//
//  Created by cygarm on 15/8/19.
//  Copyright (c) 2015年 cygarm. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
	@IBOutlet weak var imageView: UIImageView!
	@IBOutlet weak var amountSlider: UISlider!
	
	var context: CIContext!
	var filter: CIFilter!
	var beginImage: CIImage!

	override func viewDidLoad() {
		super.viewDidLoad()
		// Do any additional setup after loading the view, typically from a nib.
		
		
		/*
		1.This line creates an NSURL object that holds the path to your image file.
		2.Next you create your CIImage with the CIImage(contentsOfURL:) constructor.
		3.Next you’ll create your CIFilter object. The CIFilter constructor takes the name of the filter, and a dictionary that specifies the keys and values for that filter. Each filter will have its own unique keys and set of valid values. The CISepiaTone filter takes only two values, the KCIInputImageKey (a CIImage) and the kCIInputIntensityKey, a float value between 0 and 1. Here you give that value 0.5. Most of the filters have default values that will be used if no values are supplied. One exception is the CIImage, this must be provided as there is no default.
		4.Getting a CIImage back out of a filter is as easy as using the outputImage property. Once you have an output CIImage, you will need to convert it into a UIImage. The UIImage(CIImage:) constructor creates a UIImage from a CIImage. Once you’ve converted it to a UIImage, you just display it in the image view you added earlier.
		*/
		// 1
		let fileURL = NSBundle.mainBundle().URLForResource("image", withExtension: "png")
		
		// 2
		beginImage = CIImage(contentsOfURL: fileURL)
		
		// 3
		filter = CIFilter(name: "CISepiaTone")
		filter.setValue(beginImage, forKey: kCIInputImageKey)
		filter.setValue(0.5, forKey: kCIInputIntensityKey)
		
//		// 4
//		let newImage = UIImage(CIImage: filter.outputImage)
//		self.imageView.image = newImage
		
		/*
		4(1).Here you set up the CIContext object and use it to draw a CGImage. The CIContext(options:) constructor takes an NSDictionary that specifies options such as the color format, or whether the context should run on the CPU or GPU. For this app, the default values are fine and so you pass in nil for that argument.
		4(2).Calling createCGImage(outputImage:fromRect:) on the context with the supplied CIImage will return a new CGImage instance.
		4(3).Next, you use the UIImage(CGImage:) constructor to create a UIImage from the newly created CGImage instead of directly from the CIImage as before. Note that there is no need to explicitly release the CGImage after we are finished with it, as there would have been in Objective-C. In Swift, ARC can automatically release Core Foundation objects.
		*/
		// 4(1)
		context = CIContext(options: nil)
//		let outputImage = filter.outputImage
		
//		// 4(2)
//		let cgimg = context.createCGImage(filter.outputImage, fromRect: filter.outputImage.extent())
		
//		// 4(3)
//		let newimage = UIImage(CGImage: cgimg)
//		self.imageView.image = newimage
	}

	/*
	You’ll notice that you’ve changed the argument type from AnyObject to UISlider in the method definition. You know you’ll only be using this method to retrieve values from your UISlider, so you can go ahead and make this change. If you’d left it as AnyObject, you’d need to cast it to a UISlider or the next line would throw an error.
	*/
	@IBAction func amountSliderValueChanged(sender: UISlider) {
		let sliderValue = sender.value
		
		filter.setValue(sliderValue, forKey: kCIInputIntensityKey)
		let outputImage = filter.outputImage
		
		let cgimg = context.createCGImage(outputImage, fromRect: outputImage.extent())
		
		let newImage = UIImage(CGImage: cgimg)
		self.imageView.image = newImage
	}
	
	override func didReceiveMemoryWarning() {
		super.didReceiveMemoryWarning()
		// Dispose of any resources that can be recreated.
	}


}

